package com.dongdongshop.service.impl;

import com.dongdongshop.pojo.TbItem;
import com.dongdongshop.pojo.TbOrderItem;
import com.dongdongshop.service.CartService;
import com.dongdongshop.service.ItemService;
import com.dongdongshop.vo.Cart;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
@DubboService
public class CartServiceImpl implements CartService {

    @DubboReference
    private ItemService itemService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<Cart> addCart(Long itemId, Integer num, List<Cart> cartList) {
        TbItem tbItem = itemService.selectById(itemId);
        Cart cart = selectBySellerId(cartList, tbItem);
        if (cart == null) {
            Cart c = new Cart();
            c.setSellerId(tbItem.getSellerId());
            c.setSellerName(tbItem.getSeller());
            List<TbOrderItem> list = new ArrayList<>();
            list.add(setOrderValue(tbItem, num));
            c.setOrderItemList(list);
            cartList.add(c);
        }else {
            TbOrderItem to = selectByItemId(cart, itemId);
            if (to == null) {
                cart.getOrderItemList().add(setOrderValue(tbItem, num));
            }else {
                to.setNum(to.getNum() + num);
                to.setTotalFee(new BigDecimal(to.getPrice().doubleValue() * to.getNum()));
                if (to.getNum() <= 0) {
                    cart.getOrderItemList().remove(to);
                }
                if (cart.getOrderItemList().size() <= 0) {
                    cartList.remove(cart);
                }
            }
        }

        return cartList;
    }

    private TbOrderItem selectByItemId(Cart cart, Long itemId) {
        List<TbOrderItem> tbOrderItems = cart.getOrderItemList();
        for (TbOrderItem td : tbOrderItems) {
            if (td.getItemId().equals(itemId)) {
                return td;
            }
        }
        return null;
    }

    public Cart selectBySellerId(List<Cart> cartList, TbItem tbItem) {
        for (Cart cart : cartList) {
            if (cart.getSellerId().equals(tbItem.getSellerId())) {
                return cart;
            }
        }
        return null;
    }

    private TbOrderItem setOrderValue(TbItem tbItem, Integer num) {
        TbOrderItem tbOrderItem = new TbOrderItem();
        tbOrderItem.setGoodsId(tbItem.getGoodsId());
        tbOrderItem.setTitle(tbItem.getTitle());
        tbOrderItem.setPrice(tbItem.getPrice());
        tbOrderItem.setTotalFee(new BigDecimal(tbItem.getPrice().doubleValue() * num));
        tbOrderItem.setPicPath(tbItem.getImage());
        tbOrderItem.setNum(num);
        tbOrderItem.setItemId(tbItem.getId());
        tbOrderItem.setSellerId(tbItem.getSellerId());
        return tbOrderItem;
    }

    @Override
    public void saveToRedis(String username, List<Cart> cartList) {

        redisTemplate.boundHashOps("cart").put(username, cartList);
    }

    @Override
    public List<Cart> getCartListFormRedis(String username) {
        List<Cart> cartList = (List<Cart>) redisTemplate.boundHashOps("cart").get(username);
        return cartList == null || cartList.size() == 0 ? new ArrayList<>() : cartList;
    }

    @Override
    public List<Cart> mergeCartList(List<Cart> cartListCookie, List<Cart> cartListRedis) {
        for (Cart cartListCooki : cartListCookie) {
            for (TbOrderItem tbOrderItem : cartListCooki.getOrderItemList()) {
                cartListRedis = addCart(tbOrderItem.getItemId(), tbOrderItem.getNum(), cartListRedis);
            }
        }

        return cartListRedis;
    }

    @Override
    public TbItem getItemList(Long itemId) {
        return itemService.selectById(itemId);
    }
}
